package com.longrise.common.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.longrise.common.R;
import com.longrise.common.utils.ActivityUtil;
import com.longrise.common.utils.DZZWTools;
import com.longrise.common.utils.GenericUtil;
import com.longrise.common.utils.NetUtil;
import com.longrise.common.utils.PrintLog;
import com.umeng.analytics.MobclickAgent;


/**
 * Created by callxkj on 2017/2/13.
 * mvp的base Activity类
 */

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity {
    public final String TAG = this.getClass().getSimpleName();
    public final String UMENG_TAG = this.getClass().getName();
    public P mPresenter;
    public Context mContext;
    public RxManager mRxmanager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        beforeSetContentView();
        setContentView(getContentViewId(savedInstanceState));
        ActivityUtil.addActivity(this);
        mContext = this;
        mPresenter = GenericUtil.getT(this, 0);
        mRxmanager = new RxManager();
        if (mPresenter != null && this instanceof BaseView) {
            mPresenter.mContext = this;
            mPresenter.attachV(this);
            mPresenter.setRxManager(mRxmanager);
        }
        this.initView();
        if (mPresenter != null && this instanceof BaseView) {
            mPresenter.init();
        }
    }

    /*********************
     * 子类实现
     *****************************/
    // 获取布局文件
    public abstract int getContentViewId(@Nullable Bundle savedInstanceState);

    //初始化view
    public abstract void initView();
    /*******************************************************/

    /**
     * 设置contentView之前的一些配置
     */
    public void beforeSetContentView() {
        //初始化主题
        initTheme();
        //没有title
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        //默认竖屏
        orientationToPortrait();
        // 设置状态栏颜色
        setStatusBarColor();
    }

    public void initTheme() {
        setTheme(R.style.AppCompatThem);
    }

    public void setStatusBarColor() {
    }

    /**
     * 竖屏
     */
    protected void orientationToPortrait() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    /**
     * 横屏
     */
    protected void orientationToLandscape() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    /**
     * 全屏
     */
    protected void screenToFull() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * 状态显示状态
     * View.SYSTEM_UI_FLAG_VISIBLE 可见状态
     * View.INVISIBLE 不可见状态，Activity会伸展全屏显示
     * View.SYSTEM_UI_FLAG_FULLSCREEN Activity全屏显示，且状态栏被隐藏覆盖掉
     * View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN Activity全屏显示，但状态栏不会被隐藏覆盖，状态栏依然可见，Activity顶端布局部分会被状态遮住。
     */
    protected void setStatusBarVisiable(int visiable) {
        getWindow().getDecorView().setSystemUiVisibility(visiable);
    }

    /**
     * activity 跳转
     *
     * @param cls 需要跳转的activity
     */
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * @param cls    需要跳转的activity
     * @param bundle bundle传输数据
     */
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 显示网络加载dialog
     */
    public void showLoadingDialog() {
        DZZWTools.showWaitDialog(this);
    }

    /**
     * 隐藏网络加载dialog
     */
    public void dismissLoadingDialog() {
        DZZWTools.dismissDialog(null);
    }

    /**
     * 弹吐司
     */
    public void showToast(String msg) {
        DZZWTools.showToast(this, msg, Toast.LENGTH_SHORT);
    }

    public void showSnakbar(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            Snackbar.make(getWindow().getDecorView(), msg, Snackbar.LENGTH_SHORT).show();
        }
    }

    public boolean checkNetWorkEnable() {
        if (!NetUtil.checkNetEnable()) {
            return false;
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        //文档  http://dev.umeng.com/sdk_integate/android_sdk/analytics_doc
        if (!isFragmentActivity()) {
            MobclickAgent.onPageStart(UMENG_TAG);
        }
        MobclickAgent.onResume(this);  //统计时长
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 文档 http://dev.umeng.com/sdk_integate/android_sdk/analytics_doc
        if (!isFragmentActivity()) {
            MobclickAgent.onPageEnd(UMENG_TAG);
        }
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityUtil.removeActivity(this);
        if (mPresenter != null) {
            mPresenter.detachV();
        }
        if (mRxmanager != null) {
            mRxmanager.clear();
        }
    }

    /**
     * 用于友盟统计
     *
     * @return 如果是包含fragment的activity就重写在这个方法 return true;
     */
    public boolean isFragmentActivity() {
        return false;
    }


    //为了兼容新的baseactivity2
    public void showNonNetPage() {
    }

    //兼容baseactivity2
    public void showNormalLoadingPage() {
    }

    //兼容baseactivity2
    public void showContentPage() {
    }

}
